package com.rem.concurrency.juc.my15Lock;

import lombok.Data;

/**
 * 死锁
 *
 * @author Rem
 * @date 2021-03-11
 */

public class DeadLock {


    public static void main(String[] args) {

        String lockA = "A";
        String lockB = "B";

        new Thread(new Mythread(lockA, lockB), "T1").start();
        new Thread(new Mythread(lockB, lockA), "T2").start();

    }

}

@Data
class Mythread implements Runnable {

    private String lockA;

    private String lockB;

    public Mythread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {

        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "获得了A锁");

            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "获得了B锁");
            }
        }
    }
}
